package com.zx.kitchen.index.common.utils;

import android.content.Context;
import android.graphics.Color;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.zx.kitchen.index.R;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/3/17.
 * http://blog.csdn.net/eastmoon502136/article/details/51049111
 * 图表
 */

public class ChartUtils {
    public static ChartUtils instance = null;

    public static ChartUtils getInstance() {
        if (instance == null) {
            instance = new ChartUtils();
        }
        return instance;
    }

    public void showChart(LineChart lineChart, LineData lineData, int color) {
        lineChart.setDrawBorders(false); //在折线图上添加边框
        lineChart.setDescription(""); //数据描述
        lineChart.setNoDataTextDescription("You need to provide data for the chart.");

        lineChart.setDrawGridBackground(true); //表格颜色
        lineChart.setGridBackgroundColor(Color.WHITE & 0x70FFFFFF); //表格的颜色，设置一个透明度

        lineChart.setTouchEnabled(false); //可点击

        lineChart.setDragEnabled(false);  //可拖拽
        lineChart.setScaleEnabled(false);  //可缩放

        lineChart.setPinchZoom(false);

        lineChart.setBackgroundColor(color); //设置背景颜色

        lineChart.setData(lineData);  //填充数据

        Legend mLegend = lineChart.getLegend(); //设置标示，就是那个一组y的value的

        mLegend.setForm(Legend.LegendForm.CIRCLE); //样式
        mLegend.setFormSize(6f); //字体
        mLegend.setTextColor(Color.BLACK); //颜色

        lineChart.setVisibleXRange(1, 50);   //x轴可显示的坐标范围
        XAxis xAxis = lineChart.getXAxis();  //x轴的标示
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); //x轴位置
        xAxis.setTextColor(Color.BLACK);    //字体的颜色
        xAxis.setTextSize(10f); //字体大小
        xAxis.setGridColor(Color.GRAY);//网格线颜色
        xAxis.setDrawGridLines(false); //不显示网格线
//        xAxis.setTypeface(mTf);

        YAxis axisLeft = lineChart.getAxisLeft(); //y轴左边标示
        YAxis axisRight = lineChart.getAxisRight(); //y轴右边标示
        axisLeft.setTextColor(Color.BLACK); //字体颜色
        axisLeft.setTextSize(10f); //字体大小
        axisLeft.setAxisMaxValue(1000f); //最大值
        axisLeft.setLabelCount(6, true); //显示格数
        axisLeft.setGridColor(Color.BLACK); //网格线颜色
//        axisLeft.setTypeface(mTf);

        axisRight.setDrawAxisLine(false);
        axisRight.setDrawGridLines(false);
        axisRight.setDrawLabels(false);

        lineChart.animateX(2500);  //立即执行动画
    }

    public LineData getLineData(ArrayList<String> xValues, ArrayList<Entry> yValues, Context mContext) {
        // y轴的数据集合
        LineDataSet lineDataSet = new LineDataSet(yValues, " ");
        //用y轴的集合来设置参数
        lineDataSet.setLineWidth(1.75f); // 线宽
        lineDataSet.setCircleSize(1f);// 显示的圆形大小
        lineDataSet.setColor(mContext.getResources().getColor(R.color.chartLine));// 显示颜色
        lineDataSet.setCircleColor(mContext.getResources().getColor(R.color.chartLine));// 圆形的颜色
        lineDataSet.setHighLightColor(Color.GRAY); // 高亮的线的颜色
        lineDataSet.setHighlightEnabled(true);
        lineDataSet.setValueTextColor(mContext.getResources().getColor(R.color.orange)); //数值显示的颜色
        lineDataSet.setValueTextSize(8f);     //数值显示的大小
        ArrayList<LineDataSet> lineDataSets = new ArrayList<LineDataSet>();
        lineDataSets.add(lineDataSet); // 添加数据集合
        //创建lineData
        if (xValues.size() < 2) {
            return null;
        }
        LineData lineData = new LineData(xValues, lineDataSets);
        return lineData;
    }

}
